package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.adapter.ICanLendAdapter;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.StuffListBean;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.LoadingDialog;
import com.magicborrow.views.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;

public class ICaLendActivity extends BaseActivity implements View.OnClickListener, VolleyTool.HTTPListener {

    private ImageView imvBack;
    private RefreshRecyclerView refreshRecyclerView;
    private ArrayList<StuffBean> data;
    private ICanLendAdapter adapter;
    private GridLayoutManager mGridLayoutManager;
    private int curretPage;
    private LoadingDialog loadingDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ican_lend);

        imvBack = (ImageView) findViewById(R.id.imv_back);
        findViewById(R.id.btn_ok).setOnClickListener(this);
        refreshRecyclerView = (RefreshRecyclerView) findViewById(R.id.refreshRecyclerView);
        findViewById(R.id.btn_add).setOnClickListener(this);
        mGridLayoutManager = new GridLayoutManager(this, 2);
        refreshRecyclerView.setLayoutManager(mGridLayoutManager);
        data = new ArrayList<>();
        adapter = new ICanLendAdapter(data);
        refreshRecyclerView.setAdapter(adapter);
        initData();
        refreshRecyclerView.setOnPullListener(new RefreshRecyclerView.onPullListener() {
            @Override
            public void onPullListener() {
                curretPage++;
                initData();
            }
        });
        refreshRecyclerView.setOnRefreshListener(new RefreshRecyclerView.OnRefreshListener() {
            @Override
            public void onRefreshListener() {
                curretPage = 0;
                initData();
            }
        });
    }

    private void initData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("userId", UserTools.getUser(this).getId() + "");
        params.put("page", curretPage + "");
        params.put("size", "30");
        VolleyTool.get(Constants.URL_GET_PUBLISH_LIST, params, this, 0, StuffListBean.class);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                //TODO implement
                int posion = adapter.getCheckedPosition();
                if (posion < 0) {
                    Toast.makeText(ICaLendActivity.this, "您尚未选择商品", Toast.LENGTH_SHORT).show();
                    return;
                }
                HashMap<String, String> params = new HashMap<>();
                params.put("borrowId", ""+getIntent().getIntExtra("borrowId", 0));
                params.put("wareId", ""+data.get(posion).getId());
                params.put("acc_token", UserTools.getUser(this).getAcc_token() + "");
                if (loadingDialog == null) {
                    loadingDialog = new LoadingDialog(this);
                }
                loadingDialog.show();
                VolleyTool.get(Constants.URL_POST_BORROW_GIVE, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        loadingDialog.dismiss();
                        BaseBean bean = (BaseBean) t;
                        if (bean.getCode() >= 0) {
                            showShortMsg("消息已发送给对方");
                            finish();

                        } else {
                            showShortMsg(bean.getMessage());
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        loadingDialog.dismiss();
                    }
                }, 0, BaseBean.class);
                break;
            case R.id.btn_add:
                //TODO implement
                Intent intent = new Intent(this, PushStuffActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        refreshRecyclerView.setLoadFinish();
        StuffListBean listBean = (StuffListBean) t;
        if (listBean.getCode() >= 0) {
            if (curretPage == 0)
                data.clear();
            data.addAll(listBean.getData().getContent());
            refreshRecyclerView.setIsLastPage(listBean.getData().isLast());
            adapter.notifyDataSetChanged();

        } else {
            showShortMsg(listBean.getMessage());
            if (curretPage != 0)
                curretPage--;
        }

    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        refreshRecyclerView.setLoadFinish();
        if (curretPage != 0)
            curretPage--;
    }
}
